package idv.arthur.work;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManipulateGridReq extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ManipulateGridReq() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		
		int localSysNo = 0;
		
		localSysNo = wid.getLocalSysNo( conn, ps, rs);
		
		String strSQL = "";
		String oper = request.getParameter("oper");
		String reqSN = "";
		String reqId = "";
		String result = "";
		final String currentUser = request.getParameter("currentUser");
		
		try {
			/** 判別需進行何種操作 **/
			if ( "del".equalsIgnoreCase(oper) ) {
				reqSN = request.getParameter("reqSN");
				strSQL = "DELETE FROM reqs WHERE reqSN=?";
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, reqSN);
				ps.execute();
				wid.keepLogProfile(wid.REQUIREMENT, wid.DELETE, currentUser, conn, ps);
				result = "刪除了"+reqSN;
			} else if ( "add".equalsIgnoreCase(oper) ) {
				reqId = wid.UI2DB( request.getParameter("reqId"),conn.getMetaData().getDatabaseProductName() );
				String reqDesc = wid.UI2DB( request.getParameter("reqDesc"),conn.getMetaData().getDatabaseProductName() );
				strSQL = "INSERT INTO reqs (reqSN,reqid,reqdesc,creator,createdDateTime,sysSN) VALUES ("+wid.getCurrentSN("reqs", conn, ps, rs)+",?,?,?,?,?)";
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, reqId);
				ps.setString(2, reqDesc);
				ps.setString(3, currentUser);
				ps.setTimestamp(4, wid.currentTimeStamp());
				ps.setInt(5, localSysNo);
				ps.execute();
				
				wid.keepLogProfile(wid.REQUIREMENT, wid.ADD, currentUser, conn, ps);
				result = "新增完成 "+reqId;
				
			} else if ( "edit".equalsIgnoreCase(oper) ) {
				reqSN = request.getParameter("reqSN");
				reqId = wid.UI2DB( request.getParameter("reqId"),conn.getMetaData().getDatabaseProductName() );
				String reqDesc = wid.UI2DB( request.getParameter("reqDesc"),conn.getMetaData().getDatabaseProductName() );
				strSQL = "UPDATE reqs SET reqid=?,reqDesc=?,lastUpdator=?,lastUpdatedDateTime=? WHERE reqSN=?";
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, reqId);
				ps.setString(2, reqDesc);
				ps.setString(3, currentUser);
				ps.setTimestamp(4, wid.currentTimeStamp());
				ps.setString(5, reqSN);
				ps.execute();
				
				wid.keepLogProfile(wid.REQUIREMENT, wid.UPDATE, currentUser, conn, ps);
				result = "修改完成 "+reqId;
			}
		} catch ( UnsupportedEncodingException e ) {
			result = "異動"+reqId+"失敗!!\n\n";
			result += wid.kem(conn, ps, e, wid.toEType("UnsupportedEncodingException")+errPars, wid.isPrint2Console );
		} catch ( SQLException e) {
			result = "異動"+reqId+"失敗!!\n\n";
			result += wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch ( Exception e) {
			result = "異動"+reqId+"失敗!!\n\n";
			result += wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		response.getWriter().write(result);		
	}
}